'use strict';

angular.module('tvshowsApp')
    .controller('HistoriqueController', function ($scope, Historique) {
        $scope.historiques = [];
        $scope.loadAll = function() {
            Historique.query(function(result) {
               $scope.historiques = result;
            });
        };
        $scope.loadAll();

        $scope.create = function () {
            Historique.save($scope.historique,
                function () {
                    $scope.loadAll();
                    $('#saveHistoriqueModal').modal('hide');
                    $scope.clear();
                });
        };

        $scope.update = function (id) {
            Historique.get({id: id}, function(result) {
                $scope.historique = result;
                $('#saveHistoriqueModal').modal('show');
            });
        };

        $scope.delete = function (id) {
            Historique.get({id: id}, function(result) {
                $scope.historique = result;
                $('#deleteHistoriqueConfirmation').modal('show');
            });
        };

        $scope.confirmDelete = function (id) {
            Historique.delete({id: id},
                function () {
                    $scope.loadAll();
                    $('#deleteHistoriqueConfirmation').modal('hide');
                    $scope.clear();
                });
        };

        $scope.clear = function () {
            $scope.historique = {dateUpdated: null, id: null};
        };
    });
